﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

namespace 实验十第一题
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnWrite_Click(object sender, EventArgs e)
        {
            string xmlFilePath = Server.MapPath("Customers4Write.xml");//设置要写入的XML文件的路径

            try
            {

                XmlWriterSettings settings = new XmlWriterSettings();//创建XmlWriterSettings对象，以便设置要创建的XML文档的格式
                settings.Indent = true;//设置XML文档为有缩进
                settings.IndentChars = "\t";//设置缩进字符
                settings.Encoding = System.Text.Encoding.UTF8;//设置文件编码

                XmlWriter writer = XmlWriter.Create(xmlFilePath, settings);//创建XmlWriter对象

                writer.WriteStartDocument();
                writer.WriteStartElement("Customers");//Customers开始标记
                writer.WriteStartElement("Customer");
                writer.WriteAttributeString("CustomerID", txtCustomerID.Text);//写入CustomerID属性及属性值
                writer.WriteStartElement("CustomerInfo");
                writer.WriteElementString("Name", txtName.Text);
                writer.WriteStartElement("Email");//写入Email的开始标记
                writer.WriteString(txtEmail.Text);//写入Email的文本值
                writer.WriteEndElement();//写入Email的结束标记
                //以上三行等效于 writer.WriteElementString("Email", txtEmail.Text);
                writer.WriteStartElement("Address");
                writer.WriteElementString("Street", txtStreet.Text);
                writer.WriteElementString("City", txtCity.Text);
                writer.WriteElementString("State", txtState.Text);
                writer.WriteElementString("ZipCode", txtZipCode.Text);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteStartElement("Orders");
                writer.WriteStartElement("Order");
                writer.WriteStartElement("OrderDetails");
                writer.WriteAttributeString("OrderNumber", txtOrderNumber.Text);
                writer.WriteAttributeString("OrderDate", txtOrderDate.Text);
                writer.WriteStartElement("OrderItems");
                writer.WriteStartElement("OrderItem");
                writer.WriteAttributeString("SKUNumber", txtSKUNumber.Text);
                writer.WriteAttributeString("ItemNumber", txtItemNumber.Text);
                writer.WriteElementString("ItemCost", txtItemCost.Text);
                writer.WriteElementString("ItemPrice", txtItemPrice.Text);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Flush();//清除XmlWriter对象缓冲区中的数据
                writer.Close();//关闭XmlWriter对象
                lblInfo.Text = "文件已写入成功";
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
            }
        }
    }
}
